package com.shuimin.service;

import com.shuimin.service.model.customer_service.SurveyOrder;
import com.shuimin.service.model.customer_service.SurveyOrderDetail;
import com.shuimin.service.model.customer_service.SurveyTemp;
import com.shuimin.service.service.survey.SurveyOrderService;
import pond.common.S;
import pond.db.Record;
import pond.db.sql.SqlSelect;
import pond.web.Render;
import pond.web.Router;

import static pond.db.sql.Sql.selectFromRequest;

/**
 * Created by sky on 2017/6/8.
 */
public class SURVEY_ORDER extends Router {

    {

        get("/search", (req, resp) -> App.DB.post(t -> {
            SqlSelect sqlSelect = selectFromRequest(req.toMap(), SurveyOrder.class);
            resp.render(Render.page(
                    S._for(
                            t.query(SurveyOrder.class, sqlSelect))
                            .map(SurveyOrder::view)
                            .toList(),
                    t.count(sqlSelect.count())));
        }));
        post("/build_order_by_tmpl/:tmpl_id", (req, resp) -> {
            String tmpl_id = req.paramNonBlank("tmpl_id");
            App.DB.post(t -> {
                SurveyOrder so = SurveyOrderService.buildByTmpl(tmpl_id, t);
                resp.render(Render.json(so));
            });
        });
        get("/:id", (req, resp) -> {
            String id = req.paramNonBlank("id");
            resp.render(Render.json(App.DB.get(t -> SurveyOrderService.getById(id, t))));
        });

        post("/:order_id", (req, resp) -> {
            SurveyOrder so= (SurveyOrder) Record.newEntity(SurveyOrder.class).merge(req.toMap());
            App.DB.post(t->t.recordUpdate(so));
            resp.send(200);
        });
        post("/detail/:order_id",(req,resp)->{
            SurveyOrderDetail sod= (SurveyOrderDetail) Record.newEntity(SurveyOrderDetail.class).merge(req.toMap());
            sod.put("main_id",req.param("order_id"));
            App.DB.post(t -> t.recordUpdate(sod));
            resp.send(200);
        });
    }
}
